package com.redstar.study;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.deaux.fan.FanView;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.SearchManager;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Dictionary extends SherlockActivity {
	private DataBaseHelper myDbHelper;
	private List<Word> listOfWords;
	private GoogleAnalyticsTracker tracker;	
	private FanView fan;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setTitle("Dictionary");

		fan = (FanView) findViewById(R.id.fan_view);
		fan.setViews(R.layout.dictionary, R.layout.fan);
		
		String[] fanOptions = getResources().getStringArray(R.array.list);    
		FanMenuAdapter fanAdapter = new FanMenuAdapter(this, fanOptions);
		
		ListView fanList = (ListView) findViewById(R.id.fan_list);
		fanList.setAdapter(fanAdapter);
		/*
		fanList.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long ID) {
				// TODO Auto-generated method stub
				
				Intent i;
				
				switch (position) {
				case 0:
					i = new Intent(Dictionary.this, PracticePreferences.class);
					break;
				case 1:
					i = new Intent(Dictionary.this, QuizPreferences.class);
					break;
				case 2:
					i = new Intent(Dictionary.this, Dictionary.class);
					break;
				default:
					i = new Intent();
				}
				Dictionary.this.startActivity(i);
				
			}
			
		});*/
		
		// Start the tracker in manual dispatch mode...
		tracker = GoogleAnalyticsTracker.getInstance();
	    tracker.startNewSession("UA-29340571-1",20, this);

		ListView list = (ListView)findViewById(R.id.dict_list);
		list.setVerticalFadingEdgeEnabled(true); // Make sure that fading is displayed on samsung devices
		listOfWords = getWords();

		DictionaryAdapter adapter = new DictionaryAdapter(this, listOfWords);
		list.setAdapter(adapter);
		/*list.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				Intent i = new Intent(Dictionary.this, WebView.class);
				i.putExtra("Definition", listOfWords.get(position).getDefinition());
				Dictionary.this.startActivity(i);
			}
			
		});*/
		
		tracker.trackPageView("/Dictionary");
	}
	
	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
	    switch (item.getItemId()) {
        case android.R.id.home:
            // display menu
        	displayMenu();
            return true;
        default:
            return super.onOptionsItemSelected(item);
	    }
	}	

	private void displayMenu() {
		// TODO Auto-generated method stub
		fan.showMenu();	
	}

    public void unclick(View v) {
    	System.out.println("CLOSE");
    	fan.showMenu();
    }
    
    public void click(View v) {
    	System.out.println("OPEN");
    	fan.showMenu();
    }
	/* @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
        menu.add("Search")
            .setIcon(R.drawable.ic_search_inverse)
            .setActionView(R.layout.collapsible_edittext)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        
        EditText edit = (EditText) findViewById(R.id.AbEdit);
        edit.setOnKeyListener(new OnKeyListener(){

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				return false;
			}
        	
        });
        return true;
    }	*/
    
	private List<Word> getWords() {
		// TODO Auto-generated method stub
		listOfWords = new ArrayList<Word>();
		myDbHelper = new DataBaseHelper(this);
		createDB();

		Intent intent = getIntent();
		//check see if the intent was a search
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			//get the query from the intent
			String query = intent.getStringExtra(SearchManager.QUERY);
			//search the database for the query
			listOfWords = myDbHelper.getVocabSearch(query);
		}
		else{
			//otherwise its not a search and get all vocab
			listOfWords = myDbHelper.getAllVocab(false);
		}
		
		myDbHelper.close();
		
		return listOfWords;
	}

	private void createDB() {
		// first try to create the database if it doesnt exist
		try {
			myDbHelper.createDataBase();
		}
		catch (IOException ioe) {
			throw new Error("Unable to create database" + ioe.getMessage());
		}
		
		try {
			myDbHelper.openDataBase();
		}
		catch(SQLException sqle){
			throw sqle;
		}
	}
	

    
	@Override
    public void onDestroy() {
        super.onDestroy();
        myDbHelper.close();
        // Stop the tracker when it is no longer needed.
        tracker.stopSession();
	}
}